$(document).ready(function(){

    
    //导航切换
    $('.nav-item').on('click',function(){
        $('.nav-item').removeClass('active');
        $(this).addClass('active');
        var index = $(this).index();
        // console.log(index);
        $('.item-box').css('display','none');
        var itemBox = document.getElementsByClassName('item-box');
        itemBox[index].style.display = 'block';
    })
    /***
     * 健康教育 begin
     ***/

    var imgurl;
    var imgs = [];  //商家展示图片
    var imgArr = imgUploadShow('content-img-select','content-img-show');
    //新图删除
    $('#content-img-show').on('click','.cancle',function(){
        var index = $(this).parent().index();
        imgArr.splice(index,1);
        var contentImgList = document.getElementById('content-img-show');
        // console.log(contentImgList);
        var contentNewImgList = contentImgList.getElementsByClassName('new')[0];
        var contentImgItem = document.getElementsByClassName('content-img-item');
        // console.log(contentImgItem);
        contentNewImgList.removeChild(contentImgItem[index])

    })
    //旧图删除
    $('#content-img-show').on('click','.del',function(){
        var index = $(this).parent().index();
        // console.log(index);
        imgs.splice(index,1);
        var contentImgList = document.getElementById('content-img-show');
        // console.log(contentImgList);
        var contentNewImgList = contentImgList.getElementsByClassName('old')[0];
        var contentImgItem = document.getElementsByClassName('content-old-img-item');
        // console.log(contentImgItem);
        contentNewImgList.removeChild(contentImgItem[index])
        // console.log(imgs);
    })
    $('#upload-content-img').on('click',function(){
        allImgUpload(imgArr,setContentImgUrl)
    });
    function setContentImgUrl(res){
        if(res.length != 0){
            imgurl = {
                url:res
            } ;
            imgs.push(imgurl)
            $('#upload-content-img').css('display','none');
        }
    }
    
    $('#EducationModal').on('show.bs.modal',function(event){
        var modal = $(this);
        var btnThis = $(event.relatedTarget);
        var type = btnThis.data('type');
        if(type == '1' || type == '2'){
            // console.log(type)
            modal.find('#upType').val(type);
            modal.find('#educationId').val('');
            var title = modal.find('#title');
            title.val('');
            var author = modal.find('#author');
            author.val('');
            var video = modal.find("#video");
            var videoAre = modal.find("#videoAre");
            video.val('');
            videoAre.css('display','none')
            var switchBtn = modal.find('.switchBtn');
            var educationImgAre = modal.find("#educationImgAre")
            educationImgAre.css('display','block')
            modal.find('#content-img-show .old').html('');
            modal.find('#content-img-show .new').html('');//可能存在bug
            modal.find("#shop-img-show").html(`<div>图片预览</div>`);
            var summernote = modal.find("#summernote")
            summernote.css('display','block')
            switchBtn.css('border','1px solid #b4b4b4')
            switchBtn.children('.round').css('background','#b4b4b4')
            switchBtn.eq(0).css('border','1px solid #0b67ff');
            switchBtn.eq(0).children('.round').css('background','#0b67ff');
            $('.summernote').summernote({
                height: 300,
                lang: 'zh-CN',
                onImageUpload: function(files, editor, $editable) {
                    sendFile(files[0],editor,$editable);
                }
            })
            $('.summernote').code('');
            $('#upload-content-img').css('display','none');
            //根据type判断是2修改还是1新增
            if(type == 1){
                modal.find('#educationTxt').text('添加内容');
            }
            else if(type == 2){
                modal.find('#educationTxt').text('修改内容');
                var educationId = btnThis.data('id')
                modal.find('#educationId').val(educationId);
                var educationInfo = '';
                for(var i in educationList){
                    if(educationList[i].id == educationId){
                        educationInfo = educationList[i]
                        break;
                    }
                }
                console.log(educationInfo)
                t = educationInfo.type                        
                title.val(educationInfo.name);
                author.val(educationInfo.up_name);
                if(t == 1){
                    //文章
                    $("#videoAre").css('display','none')
                    $("#educationImgAre").css('display','block')
                    $("#summernote").css('display','block')
                    $('.summernote').code(educationInfo.content);
                    
                    imgs = educationInfo.head_img;
                    var head_img = educationInfo.head_img;
                    // console.log(head_banner);
                    var html = '';
                    for(var j in head_img){
                        html+= `
                            <div class="content-old-img-item">
                                <img src="${head_img[j].url}" alt="">
                                <div class="del themeColor">删 除</div>
                            </div>
                        `
                    }
                    modal.find('#content-img-show  .old').html(html);
                }else if(t == 2){
                    //视频
                    switchBtn.css('border','1px solid #b4b4b4')
                    switchBtn.children('.round').css('background','#b4b4b4')
                    switchBtn.eq(1).css('border','1px solid #0b67ff');
                    switchBtn.eq(1).children('.round').css('background','#0b67ff');
                    $("#videoAre").css('display','block')
                    $("#educationImgAre").css('display','none')
                    $("#summernote").css('display','none')
                    video.val(educationInfo.url)
                }
            }
        }
        
    })

    
    //切换按钮
    var t = 1
    $(".switchBtn").on('click',function(){
        var type = $(this).data('type')
        // console.log(type)
        if(type == 'article'){
            //文章
            $("#videoAre").css('display','none')
            $("#educationImgAre").css('display','block')
            $("#summernote").css('display','block')
            t = 1
            
        }else if(type == 'video'){
            //视频
            $("#videoAre").css('display','block')
            $("#educationImgAre").css('display','none')
            $("#summernote").css('display','none')
            t = 2
        }
        
        $('.switchBtn').css('border','1px solid #b4b4b4')
        $('.switchBtn').children('.round').css('background','#b4b4b4')
        $(this).css('border','1px solid #0b67ff')
        $(this).children('.round').css('background','#0b67ff')
    })

    //提交按钮
    $("#educationBtn").on('click',function(){
        var title = $("#title").val()
        var author = $("#author").val()
        if(title == ''||author == ''){
            alert('请将信息填写完整！')
            return
        }
        var type = $("#upType").val();
        console.log(t)
        if(type == 1){
            if(t == 1){
                var data = {}
                //文章
                var content = $('.summernote').code()
                if(imgs.length == 0){
                    alert('请上传图片！')
                    return
                }
                if(!content){
                    alert('请填写视频文章内容！')
                    return
                }
                data = {
                    "name": title,
                    "up_name": author,
                    "type": t,
                    "status": 0,
                    "see": 0,
                    "createtime": setTime(new Date()),
                    "content": content,
                    "community_id": 0,
                    "head_img": imgs
                }
            }else if(t == 2){
                //视频
                var video = $('#video').val()
                console.log(video)
                if(!video){
                    alert('请填写视频路径！')
                    return
                }
                data = {
                    "name": title,
                    "up_name": author,
                    "type": t,
                    "status": 0,
                    "see": 0,
                    "createtime": setTime(new Date()),
                    "url": video,
                    "community_id": 0,
                }
            }
        }else if(type == 2){
            var id = $("#educationId").val();
            if(t == 1){
                //文章
                var content = $('.summernote').code()
                // console.log(imgs)
                if(imgs.length == 0){
                    alert('请上传图片！')
                    return
                }
                if(!content){
                    alert('请填写视频文章内容！')
                    return
                }
                data = {
                    "id": id,
                    "name": title,
                    "up_name": author,
                    "type": t,
                    "status": 0,
                    "see": 0,
                    "createtime": setTime(new Date()),
                    "content": content,
                    "community_id": 0,
                    "head_img": imgs
                }
            }else if(t == 2){
                //视频
                var video = $('#video').val()
                if(!video){
                    alert('请填写视频路径！')
                    return
                }
                data = {
                    "id": id,
                    "name": title,
                    "up_name": author,
                    "type": t,
                    "status": 0,
                    "see": 0,
                    "createtime": setTime(new Date()),
                    "url": video,
                    "community_id": 0,
                }
            }
        }
        // console.log(content)
        data = JSON.stringify(data);
        // console.log(data);
        var text = '';
        var text2 = t==1?'文章':'视频'
        if(type == 2){
            text = text2 + '信息，修改成功！';
        }else if(type == 1){
            text = '新的'+ text2 +'，添加成功！';
        }
        postAjax(`${global.apiUrl}/admin_add_community_health_new_video`,function(res){
            if(res == 1){
                alert(text);
                $('#EducationModal').modal('hide');
                getEducationList(educationPageNo,showNum)                
            }
        },{
            type:type,
            t: t,
            pro: data
        })
    })

     //获取文章视频列表
     var educationPageNo = 1,showNum=10;
     var educationList = '';
     getEducationList(educationPageNo,showNum)
     function getEducationList(p,showNum){
        var data = [0]
        data = JSON.stringify(data)
        console.log(data)
        getAjax(`${global.apiUrl}/get_community_health_new_video_all`,function(res){
            console.log(res);
            if(res.list.length == 0){
                $("#page2").paging({
                    pageNo:0,
                    totalPage: 0, //总页数
                    totalSize: 0,
                    callback: function(num) {
                    getEducationList(num,showNum)
                    }
                });
                var html = `
                        <tr>
                            <td colspan="5" align="center">暂无文章视频</td>
                        </tr>`
                $('#Education-List').html(html);
            }else{
                $("#page2").paging({
                    pageNo:p,
                    totalPage: Math.ceil(res.count/showNum), //总页数
                    totalSize: res.count,
                    callback: function(num) {
                    getEducationList(num,showNum)
                    educationPageNo = num
                    }
                });
                var html = '';
                educationList = res.list;
                var list = res.list;
                for(var i in list){
                    html += `
                        <tr>
                        <td>${list[i].name}</td>
                        <td>${list[i].createtime}</td>
                        <td>${list[i].up_name}</td>
                        <td>${type(list[i].type)}</td>
                        <td class="control-td themeColor">
                            <span class="education-reset" data-id="${list[i].id}" data-mainType="${list[i].type}" data-type="2" data-target="#EducationModal" data-toggle="modal">修改</span> 
                            <span class="education-delete" style="margin-left: 15px" data-id="${list[i].id}" data-type="${list[i].type}">删除</span>
                        </td>
                        </tr>
                `
                }
                $('#Education-List').html(html);
            }
        },{
        p:p,
        num:showNum,
        community_id: data
        })
    }
    
    function type(type){
        var text = ''
        switch (type) {
            case '1':
                text = '文章'
                break;
            case '2':
                text = '视频'
                break;
        }
        return text
    }
     
    function select(status,id,type){
        var html = ''
        switch (status) {
            case '0':
                html = `<span class="education-select" style="margin-left: 15px" data-status="${status}" data-id="${id}" data-type="${type}">设为精选</span>`
                break;
            case '1':
                html = `<span class="education-select" style="margin-left: 15px" data-status="${status}" data-id="${id}" data-type="${type}">取消精选</span>`
                break;
        }
        return html
    }

    /** 删除 **/
    $('#Education-List').on('click','.education-delete',function(){
        var educationId = $(this).data('id');
        var type = $(this).data('type')
        var text = type==1?'文章':'视频'
        var f = confirm('是否删除该' + text);
        if(f){
            getAjax(`${global.apiUrl}/admin_add_community_health_new_video`,function(res){
                // console.log(res);
                if(res == 1){
                    alert(text + '删除成功！');
                    getEducationList(educationPageNo,showNum);
                }
            },{
                t: type,
                type: 3,
                id: educationId
            })
        }
    })

    /***
     * 健康教育 end
     ***/


    /***
     * 健康讲堂 start
     ***/

    $('#lectureModal').on('show.bs.modal',function(event){
        var modal = $(this);
        var btnThis = $(event.relatedTarget);
        var type = btnThis.data('type');
        modal.find('#lectureUpType').val(type);
        modal.find('#lectureId').val('');
        var title = modal.find('#title2');
        title.val('');
        var author = modal.find('#author2');
        author.val('');
        var video = modal.find("#video2");
        video.val('');
        //根据type判断是2修改还是1新增
        if(type == 1){
            modal.find('#lectureTxt').text('添加内容');
        }
        else if(type == 2){
            modal.find('#lectureTxt').text('修改内容');
            var lectureId = btnThis.data('id')
            modal.find('#lectureId').val(lectureId);
            var lectureInfo = '';
            for(var i in lectureList){
                if(lectureList[i].id == lectureId){
                    lectureInfo = lectureList[i]
                    break;
                }
            }
            // console.log(lectureInfo)
            t = lectureInfo.type                        
            title.val(lectureInfo.name);
            author.val(lectureInfo.up_name);
            video.val(lectureInfo.url)
        }
    })


    //提交按钮
    $("#lectureBtn").on('click',function(){
        var title = $("#title2").val()
        var author = $("#author2").val()
        var video = $('#video2').val()
        if(title == ''||author == '' || video == ''){
            alert('请将信息填写完整！')
            return
        }
        var type = $("#lectureUpType").val();
        // console.log('type:',type);
        if(type == 1){
            data = {
                "name": title,
                "up_name": author,
                "status": 0,
                "see": 0,
                "createtime": setTime(new Date()),
                "url": video,
                "community_id": 0,
            }
        }else if(type == 2){
            var id = $("#lectureId").val();
            data = {
                "id": id,
                "name": title,
                "up_name": author,
                "status": 0,
                "see": 0,
                "createtime": setTime(new Date()),
                "url": video,
                "community_id": 0,
            }
        }
        // console.log(content)
        data = JSON.stringify(data);
        console.log(type)
        console.log(data);
        var text = '';
        if(type == 2){
            text = '视频信息，修改成功！';
        }else if(type == 1){
            text = '新的视频，添加成功！';
        }
        postAjax(`${global.apiUrl}/Api/Communityhealth/operation_community_health_teacher_video`,function(res){
            console.log(res)
            if(res == 1){
                alert(text);
                $('#lectureModal').modal('hide');
                getLectureList(lecturePageNo,showNum)                
            }
        },{
            type:type,
            pro: data
        })
    })

    //获取文章视频列表
    var lecturePageNo = 1,showNum=10;
    var lectureList = '';
    getLectureList(lecturePageNo,showNum)
    function getLectureList(p,showNum){
        var data = []
        data.push(0)
        data = JSON.stringify(data)
        console.log(data)
        getAjax(`${global.apiUrl}/get_community_health_teacher_video`,function(res){
            console.log(res);
            if(res.list.length == 0){
                $("#page4").paging({
                    pageNo:0,
                    totalPage: 0, //总页数
                    totalSize: 0,
                    callback: function(num) {
                        getLectureList(num,showNum)
                    }
                });
                var html = `
                        <tr>
                            <td colspan="5" align="center">暂无健康讲堂视频</td>
                        </tr>`
                $('#lecture-List').html(html);
            }else{
                $("#page4").paging({
                    pageNo:p,
                    totalPage: Math.ceil(res.count/showNum), //总页数
                    totalSize: res.count,
                    callback: function(num) {
                        getLectureList(num,showNum)
                        lecturePageNo = num
                    }
                });
                var html = '';
                lectureList = res.list;
                var list = res.list;
                for(var i in list){
                    html += `
                        <tr>
                            <td>${list[i].name}</td>
                            <td>${list[i].createtime}</td>
                            <td>${list[i].up_name}</td>
                            <td class="control-td themeColor">
                                <span class="lecture-reset" data-id="${list[i].id}" data-type="2" data-target="#lectureModal" data-toggle="modal">修改</span> 
                                <span class="lecture-delete" style="margin-left: 15px" data-id="${list[i].id}" >删除</span>
                            </td>
                        </tr>
                `
                }
                $('#lecture-List').html(html);
            }
        },{
            p:p,
            num:showNum,
            community_id: data
        })
    }

    /** 删除 **/
    $('#lecture-List').on('click','.lecture-delete',function(){
        var lectureId = $(this).data('id');
        var f = confirm('是否删除该视频');
        if(f){
            getAjax(`${global.apiUrl}/Api/Communityhealth/operation_community_health_teacher_video`,function(res){
                // console.log(res);
                if(res == 1){
                    alert('该视频删除成功！');
                    getLectureList(lecturePageNo,showNum);
                }
            },{
                type: 3,
                id: lectureId
            })
        }
    })


    /***
     * 健康讲堂 end
     ***/
});
